from django.db import models
from sync.models import SFObject,SFField
from django.db.models import Q
from django.db import connection, transaction
from wizard.models.view import View
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse

class SingleView(View):
    custome_key=models.CharField(max_length=255,default="")
    class Meta:
        db_table = 'wizard_single_view'
        app_label = 'wizard'
    """
    View using functions
    """
    def resp(self,request,dev=False):
        record=self.load(request)
        if not record:
            return self.not_found(request)
        if self.custom_html:
            return render_to_response(self.custom_html, 
                              {'record':record},
                              context_instance = RequestContext(request))
        else:
            rendered=self.render(record)
            return render_to_response('wizard/views/single_view.html', 
                              {'rendered':rendered},
                              context_instance = RequestContext(request))
    
    def load(self,request):
        obj_id=request.REQUEST.get('obj_id',None)
        if not obj_id:
            return False
        return self.obj.l_filter(' lid=%s '%obj_id)
    def get_where(self):
        pass
    def get_sort(self):
        pass
    def render(self,record):
        from wizard.models.view_field import ViewField
        record=self.load(request)
        if len(record)<=0:
            return self.not_found()
        fields=ViewField.objects.filter(view=self)
        r=record[0]
        html="<table>"
        for f in fields:
            html+='<tr><td>%s</td><td>%s</td></tr>'%(f.label,""+getattr(r,f.name,""))
        html="</table>"
        return html
